﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

using log4net;
using Microsoft.Web.Mvc;
using TanHungViet.Web.Models;
using TanHungViet.Helper;

namespace TanHungViet.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static ILog _logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //routes.MapRoute(
            //    "Default", // Route name
            //    "{controller}/{action}/{code}", // URL with parameters
            //    new { controller = "TanHungViet", action = "Index", code = UrlParameter.Optional } // Parameter defaults
            //);

            routes.MapRoute(
                "MyRoute",                      // Route name
                "quanlynoidung/{action}/{code}",         // URL with parameters
                new { controller = "QuanLyNoiDung", action = "Index", code = "" } // Parameter defaults            
            );

            routes.MapRoute(
                "Default", // Route name
                "{action}/{code}", // URL with parameters
                new { controller = "TanHungViet", action = "Index", code = UrlParameter.Optional } // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
            ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());

            log4net.Config.XmlConfigurator.Configure();
            _logger.Info("Application_Start ....");

            // thong ke truy cap
            Application["CountUserOnline"] = 15;
            using (DBTanHungVietEntities entity = new DBTanHungVietEntities())
            {
                ParameterRepository paramRepository = new ParameterRepository(entity);
                Application[Constant.ParamNameUserAccessedCount] = paramRepository.GetParamValue(Constant.ParamNameUserAccessedCount);
            }

            
        }

        protected void Session_Start()
        {
            Session.Timeout = 5;
            Session["CultureInfo"] = "";

            Dictionary<int, int> cart = new Dictionary<int, int>();
            Session["CountCart"] = 0;
            Session["Cart"] = cart;

            int countUserOnline = (int)Application["CountUserOnline"];
            countUserOnline++;
            Application["CountUserOnline"] = countUserOnline;

            int userAccessedCount = int.Parse(Application[Constant.ParamNameUserAccessedCount].ToString());
            userAccessedCount++;
            Application[Constant.ParamNameUserAccessedCount] = userAccessedCount;
        }

        protected void Session_End()
        {
            int countUserOnline = (int)Application["CountUserOnline"];
            countUserOnline--;
            Application["CountUserOnline"] = countUserOnline;  
        }

        protected void Application_End()
        {
            string userAccessedCount = Application[Constant.ParamNameUserAccessedCount].ToString();
            using (DBTanHungVietEntities entity = new DBTanHungVietEntities())
            {
                ParameterRepository paramRepository = new ParameterRepository(entity);
                paramRepository.SetParamValue(Constant.ParamNameUserAccessedCount, userAccessedCount);
            }
        }
    }
}